package edu.sctu.java.advanced.pattern.proxy.lab03;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MyMethodInterceptor implements MethodInterceptor {

    public Object getProxyInstance(Class clz) {
        Enhancer en = new Enhancer();
        en.setSuperclass(clz);
        en.setCallback(this);

        return en.create();
    }

    @Override
    public Object intercept(Object target,
                            Method method,
                            Object[] args,
                            MethodProxy methodProxy) throws Throwable {
        Object returnObj;

        System.out.println(
                method.getDeclaringClass().getName() + "." +
                method.getName() + "() method invoking...");
        returnObj = methodProxy.invokeSuper(target, args);
        return returnObj;
    }


}
